package core;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class Properties {

    private static final Logger logger = LoggerFactory.getLogger(Properties.class);

    protected static JedisPool pool;

    private JedisPoolConfig config;

    protected static JedisPool getInstance(){
        if(pool == null) {
            logger.error("redis not yet initialized");
            return null;
        }
        return pool;
    }

    public Properties(int maxIdle, int maxTotal, String url, int port, int timeout, String password) {
        if(pool != null){
            logger.warn("Property has been successfully initialized,the new Properties() has lost effect");
            return;
        }
        config = new JedisPoolConfig();
        config.setMaxIdle(maxIdle);
        config.setMaxTotal(maxTotal);
        pool = new JedisPool(config, url, port, timeout, password);
    }

    public void close() {
        pool.close();
    }
}
